﻿using animexx.Models;
using animexx.OAuth;
using AsyncOAuth;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace animexx.ViewModels
{
    class ENSFolderViewModel : ViewModelBase
    {
        public async void LoadData(string id="1", string typ="an", bool archiv = false, int anzahl = 20, int seite = 0)
        {
            await LoadFolderInfo(id, typ);

            await LoadENSList(id, typ, archiv, anzahl, seite);

            this.IsDataLoaded = true;
        }

        private async Task LoadFolderInfo(string id, string typ)
        {
            var client = new HttpClient(new OAuthMessageHandler(Keys.ConsumerKey, Keys.ConsumerSecret, App.accessToken));
            string url = String.Format(Urls.BaseUrl +
                "json/ens/ordner_info?ordner_id={0}&ordner_typ={1}",
                id, typ);
            var json = await client.GetStringAsync(url);
            System.Diagnostics.Debug.WriteLine("ordner info:" + json);
            var reply = JsonConvert.DeserializeObject<JSONReply<ENSOrdner>>(json);
            _ordnerData = reply.returnValue;
            OnPropertyChanged("OrdnerData");
        }

        private async Task LoadENSList(string id, string typ, bool archiv, int anzahl, int seite)
        {
            var client = new HttpClient(new OAuthMessageHandler(Keys.ConsumerKey, Keys.ConsumerSecret, App.accessToken));
            string url = String.Format(Urls.BaseUrl +
                "json/ens/ordner_ens_liste?ordner_id={0}&ordner_typ={1}&archiv={2}&anzahl={3}&seite={4}",
                id, typ, archiv ? "true" : "false", anzahl, seite);
            var json = await client.GetStringAsync(url);
            System.Diagnostics.Debug.WriteLine("ens list:" + json);
            var reply = JsonConvert.DeserializeObject<JSONReply<List<ENS>>>(json);
            _content = reply.returnValue;
            OnPropertyChanged("Content");
        }

        private ENSOrdner _ordnerData;
        public ENSOrdner OrdnerData
        {
            get { return _ordnerData; }
            set
            {
                SetProperty(ref _ordnerData, value);
            }
        }

        private List<ENS> _content;
        public List<ENS> Content
        {
            get { return _content;  }
            set
            {
                SetProperty(ref _content, value);
            }
        }

    }
}
